function GSSOut = gs_yepoints(GSS)
%gs_yepoints - Re-enabling of points already excluded in a previous session
%
%       GSSOut = gs_yepoints(GSS)
%
% The input data are related to N experimental points, and it is
%
%   ns     : points indices;
%   e,n    : east, north coordinates; 
%   ve,vn  : displacements (or velocities) along east and north directions;
%   npe    : actual index of exclusion. 
%
% This function shows the points and the correspondent arrows and permits 
% the re-entering of points that have already excluded in a precedent 
% session of GRID_STRAIN program. The index npe(m) is 0 if the point 
% (e(m),n(m)) has been excluded and 1 otherwise. This function permits the
% transition npe(m) = 0 -> npe(m) = 1 with a interactive command on a 
% figure whose handle is gcf+1 (kf has this value).
% The figure is rougly centered in the screen and covers large part of it.
%
%       [npem,kf] = gs_yepoints(e,n,ve,vn,npe,k)
%
% The function operates as above, but, if k is a positive integer, 
% the figure generated has handle k.
%
%       [npem,kf] = gs_yepoints(e,n,ve,vn,npe,k,vset)
%
% As above, but the figure-size and position are managed by vector vset.
%
%       [npem,kf] = gs_yepoints(e,n,ve,vn,npe,k,vset,ns)
%
% As above, but the stations are numbered with the index vector ns.
%
%       [npem,kf] = gs_yepoints(e,n,ve,vn,npe,k,vset,ns,IMS)
%
% As above, but a background images is imtroduced by means of IMS.
%
% See also GS_DATALOAD, GS_NOPOINTS, GRID_STRAIN, STRAIN_ZERO.

% G. Teza, 2005, 2012, 2022.


format long e; 

ns = GSS.StationID;
stn = GSS.StationName; 
e = GSS.East;
n = GSS.North;
ve = GSS.EastVelocity;
vn = GSS.NorthVelocity;
IMS = GSS.RefImageStruct;
lv = length(e);

GSSfields = fields(GSS);
if ismember('IncludedPoints',GSSfields)
    npei = GSS.IncludedPoints;
else
    npei = [];
end

GSSOut = GSS;

Iexcl = ~npei;  % Not included points
if isempty(npei) || (sum(Iexcl) == 0)
    fprintf('\nALL THE POINTS ARE ALREADY INCLUDED \n');
    return
end
    
ei  = e;  
ni  = n;
vei = ve; 
vni = vn;
ei(Iexcl)  = NaN; 
ni(Iexcl)  = NaN;
vei(Iexcl) = NaN; 
vni(Iexcl) = NaN;

ex  = e;  
nx  = n;
vex = ve; 
vnx = vn;
Inexcl = npei;
ex(Inexcl) = NaN; 
nx(Inexcl) = NaN;
vex(Inexcl) = NaN; 
vnx(Inexcl) = NaN;

if ~isempty(ns)
    nsi = ns; 
    nsi(Iexcl) = NaN;
    nsx = nan(lv,1); 
    nsx(Iexcl) = ns(Iexcl);
    stni = [];
    stnx = [];
else
    nsi = []; 
    nsx = [];
    stni = stn;
    stnx = cell(lv,1);
end

npem = npei;
mediff = mean([std(diff(e)) std(diff(n))]);

nocont = 0;
mm = 1;
while nocont == 0
    
    if mm == 1
        %------------------------------------------------------------------
        figki = plotinette(ei,ni,vei,vni,ex,nx,vex,vnx,...
            nsi,nsx,stni,stnx,mediff,IMS,[]);
        %------------------------------------------------------------------
    else
        figki = figure;
        set(figki,'Units','normalized','OuterPosition',[0 0 1 1]); 
    end
    
    mm = mm+1;
    [x,y] = ginput(1);
    dist = sqrt((ex-x).^2+(nx-y).^2);
    [~,Imi] = min(dist);
    if length(Imi) > 1
        Imi = min(Imi); 
    end
    ep = ex(Imi);
    np = nx(Imi);
    hold on; 
    plot(ep,np,'xr'); 
    hold off;
    if isempty(ns)
        strPoint = stn{Imi};
    else
        strPoint = num2str(ns(Imi));
    end
    lb = uicontrol(figki,...
        'Style','listbox',...
        'string', {sprintf('ACCEPT FOR THE RE-INCLUSION THE POINT %s (%f E, %f N)',strPoint,ep,np)...
            'DO NOT RE-INCLUDE THIS POINT'},...
        'FontSize',14,...
        'Units','normalized','Position', [0.05 0.05 0.40 0.15],...
        'Callback', 'uiresume(gcbf)');
    uiwait(figki);
    menuc = lb.Value;
    delete(lb);
    if menuc == 1
        ei(Imi) = ep;  
        ni(Imi) = np;
        ex(Imi) = NaN; 
        nx(Imi) = NaN;
        vei(Imi) = ve(Imi); 
        vni(Imi) = vn(Imi);
        vex(Imi) = NaN;     
        vnx(Imi) = NaN;
        npem(Imi) = true;
        if ~isempty(ns)
            nsi(Imi) = ns(Imi); 
            nsx(Imi) = NaN;
        else
            stni(Imi) = stn(Imi);
            stnx(Imi) = {[]}; %#ok<*AGROW> 
        end
        %------------------------------------------------------------------
        figki = plotinette(ei,ni,vei,vni,ex,nx,vex,vnx,...
            nsi,nsx,stni,stnx,mediff,IMS,figki.Number);
        %------------------------------------------------------------------
    end
    
    if sum(isnan(ei)) > 0  % In this case, at least a point could be re-included                                       % can be excluded
        lb = uicontrol(figki,...
            'Style','listbox',...
            'string', {'RE-INCLUDE ANOTHER POINT','SKIP'},...
            'FontSize',14,...
            'Units','normalized','Position', [0.05 0.05 0.30 0.15],...
            'Callback', 'uiresume(gcbf)');
        uiwait(figki);
        menud = lb.Value;
        delete(lb);
        if menud == 2
            nocont = 1; 
        end
    else
        nocont = 1;
    end

end

close(figki);

GSSOut.IncludedPoints = npem; 

%% ANCILLARY FUNCTIONS

function figk = plotinette(ei,ni,vei,vni,ex,nx,vex,vnx,...
    nsi,nsx,stni,stnx,mediff,IMS,k)
% Plot of included points and related displacements (black color) and not 
% included points and displacements (red) on a figure with handle k.

Options = GridStrainOptions;

if ~isempty(k)
    figk = figure(k);
else
    figk = figure;
end
set(figk,'Units','normalized','OuterPosition',[0 0 1 1]); 
warning('off','all');
plot(ei,ni,'ok',ex,nx,'or');
legend('Point included','Point not included','Location','NorthEastOutside');
hold on
quiver(ei,ni,vei,vni,'k','HandleVisibility','off');   % vector visualization 
quiver(ex,nx,vex,vnx,'r','HandleVisibility','off');

if Options.VisualizePointIDs
    for kk = 1:numel(ei)
        if ~isempty(nsi) && ~isnan(nsi(kk))
            strikk = num2str(nsi(kk));
            okikk = true;
        elseif ~isempty(stni) && ~isempty(stni{kk})
            strikk = stni{kk};
            okikk = true;
        else
            okikk = false;
        end
        if okikk 
            se = sign(vei(kk)); 
            sn = sign(vni(kk));
            ht = text(ei(kk)-se*mediff/12,ni(kk)-sn*mediff/12,strikk);
            set(ht,'Color','b');
        end
    end
    for kk = 1:numel(ex)
        if ~isempty(nsx) && ~isnan(nsx(kk))
            strxkk = num2str(nsx(kk));
            okxkk = true;
        elseif ~isempty(stnx) && ~isempty(stnx{kk})
            strxkk = stnx{kk};
            okxkk = true;
        else
            okxkk = false;
        end
        if okxkk 
            se = sign(vex(kk)); sn = sign(vnx(kk));
            ht = text(ex(kk)-se*mediff/12,nx(kk)-sn*mediff/12,strxkk);
            set(ht,'Color','r');
        end
    end
end

title('CHOICE OF EXCLUDED POINTS','FontSize',14);
xlabel('EAST','FontSize',14); 
ylabel('NORTH','FontSize',14);
ALA = axis;
gs_ReadBGImage(IMS,ALA);
axis equal; 
hold off; 
set(gca,'FontSize',12);